// pages/productCollect/productCollect.js
let util = require('../../utils/util');
const api = require('../../utils/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,//是否全选 
    img: "",//图片
    title: "",//商品名称
    price:"",//价格
    proInfo: [],//wx.getStorageSync("proInfo"),//商品信息
    curIdx: [],//判断选中
    colNone: false//判断商品是否为空
  },

  //全选事件
  onCheck() {
    let that = this;
    this.setData({
      checked: !this.data.checked
    },function(){
      let len = that.data.proInfo.length;
      if(that.data.checked){
        for (let i = 0; i < len; i++) {
          that.setData({
            ["curIdx[" + i + "]"]: 1
          })
        }
      }else{
        for (let j = 0; j < len; j++) {
          that.setData({
            ["curIdx[" + j + "]"]: 0
          })
        }
      }
    })
  },

  // 商品是否选中事件
  listChange(e){
    let that = this;
    console.log(e)
    let selArr = e.detail.value;
    let len = this.data.proInfo.length;
    for(let i=0;i<len;i++ ){
      if (selArr.indexOf(i.toString())!=-1){
        that.setData({
          ["curIdx[" + i + "]"]: 1,
        })
      }else{
        that.setData({
          ["curIdx[" + i + "]"]: 0,
        })
      }
    }  
  },
// 删除事件
  onDelete(){
    let that = this;
    let newPro = [];
    if (that.data.curIdx.length == 0){
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      })
    }else{
      wx.showModal({
        title: '删除商品',
        content: '确定删除这些商品吗',
        success: function (res) {
          if (res.confirm) {
            that.data.curIdx.forEach(function (item, index) {
              if (item == 1) {
                let cur = that.data.proInfo[index].value_id;

                util.myRequest(
                  api.collectaddordelete,
                  {
                    valueId: cur,
                    typeId: "1"
                  },
                  res => {
                    util.myRequest(
                      api.collectList,
                      {
                        typeId: "1"
                      },
                      res => {
                        wx.hideLoading();
                        that.setData({
                          proInfo: res.data.data.data
                        }, function () {
                          that.setData({
                            curIdx: []
                          })
                        })
                      },
                      'GET'
                    )
                    //获取收藏夹列表信息
                  },
                  "POST"
                )
              }
            })
          }
        }
      })
    }
  },
  
  onLoad(options){
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    //获取收藏夹列表信息
    util.myRequest(
      api.collectList,
      {
        typeId: "1"
      },
      res => {
        wx.hideLoading();
        that.setData({
          proInfo: res.data.data.data
        })
        if (that.data.proInfo.length == 0) {
          that.setData({
            colNone: true
          })
        }
      },
      'GET'
    )
    // util.myRequest(
    //   api.collectaddordelete, 
    //   {
    //     valueId: "1152161",
    //     typeId: "1"
    //   },
    //   res => {
    //     console.log(res)
    //   },
    //   'POST'
    // )
  },
  
})